package com.rover022.flixelgame.manager
{
	import com.demonsters.debugger.MonsterDebugger;
	import com.smartfoxserver.v2.SmartFox;
	import com.smartfoxserver.v2.core.SFSEvent;
	import com.smartfoxserver.v2.requests.LoginRequest;
	
	import org.osflash.signals.Signal;

	public class SfsNetManager
	{
		/**
		 * smartfox server 的网络代理
		 */
		public var smartfox:SmartFox;

		public var zoneArray:Array=["StormWind", "IronForge", "TheUndercity", "ElfCity"];
		private static var _instance:SfsNetManager;

		/**
		 * 消息发送者...
		 * 负责吧消息发送到到world;如果初始化成功 world 生成2传送npc
		 * 
		 */
		public var eventSign:Signal;

		public function SfsNetManager(src:SingletonEnforcer)
		{
			eventSign=new Signal();
			initProxy();
		}

		public static function getInstance():SfsNetManager
		{
			if (null == _instance)
			{
				_instance=new SfsNetManager(new SingletonEnforcer());
			}
			return _instance;
		}

		public function initProxy():void
		{
			smartfox=new SmartFox();
			smartfox.loadConfig();
			smartfox.addEventListener(SFSEvent.CONFIG_LOAD_FAILURE, onConfigLoadFailure);
			smartfox.addEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, onConfigLoadSuccess);

			smartfox.addEventListener(SFSEvent.LOGIN, onLoginHandle);
			smartfox.addEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
			smartfox.addEventListener(SFSEvent.LOGOUT, onLoginOut);
			smartfox.addEventListener(SFSEvent.USER_COUNT_CHANGE, onUserCountChange);
			smartfox.addEventListener(SFSEvent.USER_EXIT_ROOM, onUserExitRoom);
			smartfox.addEventListener(SFSEvent.USER_ENTER_ROOM, onUserEnterRoom),

			smartfox.addEventListener(SFSEvent.CONNECTION, onConnect);
			smartfox.addEventListener(SFSEvent.CONNECTION_LOST, onConnectLost);
			smartfox.addEventListener(SFSEvent.CONNECTION_RESUME, onConnectResume);
			smartfox.addEventListener(SFSEvent.CONNECTION_RETRY, onConnectRetry);
		}

		protected function onUserEnterRoom(event:SFSEvent):void
		{

			eventSign.dispatch(event); //发送消息
		}

		protected function onUserExitRoom(event:SFSEvent):void
		{

			eventSign.dispatch(event); //发送消息
		}

		protected function onUserCountChange(event:SFSEvent):void
		{

			eventSign.dispatch(event); //发送消息
		}

		protected function onLoginOut(event:SFSEvent):void
		{

			eventSign.dispatch(event); //发送消息
		}

		protected function onLoginError(event:SFSEvent):void
		{
			eventSign.dispatch(event); //发送消息
		}


		protected function onLoginHandle(event:SFSEvent):void
		{
			if (event.params.success)
			{

			}
			else
			{

			}
			eventSign.dispatch(event); //发送消息
		}

		protected function onConnectLost(event:SFSEvent):void
		{
			if (event.params.success)
			{
				var username:String=PlayerManager.getInstance().owerUserVo.playName;
				var login:LoginRequest=new LoginRequest(username, zoneArray[0]);
				smartfox.send(login);
			}
		}

		protected function onConnect(event:SFSEvent):void
		{
			MonsterDebugger.log("网络连接丢失,请重新登陆!");
		}

		protected function onConnectRetry(event:SFSEvent):void
		{
			MonsterDebugger.log("网络重新恢复");

		}

		protected function onConnectResume(event:SFSEvent):void
		{
			// TODO Auto-generated method stub

		}


		protected function onConfigLoadSuccess(event:SFSEvent):void
		{
			MonsterDebugger.log("net config load success");

		}

		protected function onConfigLoadFailure(event:SFSEvent):void
		{
			MonsterDebugger.log("net config load failure");
		}

	}
}

class SingletonEnforcer
{
}
